package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.EnumUtils;
import com.sxbbc.common.core.entity.jz.ChickenCoupon;
import com.sxbbc.common.core.entity.jz.ChickenOrder;
import com.sxbbc.common.core.entity.jz.ChickenSubscribe;
import com.sxbbc.common.core.enums.ChickenCouponStatusEnums;
import com.sxbbc.common.core.enums.ChickenCouponUseTypeEnums;
import com.sxbbc.common.core.mapper.jz.ChickenCouponMapper;
import com.sxbbc.common.core.mapper.jz.ChickenOrderMapper;
import com.sxbbc.common.core.mapper.jz.ChickenSubscribeMapper;
import com.sxbbc.common.core.service.IChickenCouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 提鸡券信息 服务实现类
 * </p>
 *
 * @author
 * @since 2020-03-27
 */
@Service
public class ChickenCouponServiceImpl extends ServiceImpl<ChickenCouponMapper, ChickenCoupon> implements IChickenCouponService {

    @Autowired
    private ChickenCouponMapper chickenCouponMapper;
    @Autowired
    private ChickenSubscribeMapper chickenSubscribeMapper;
    @Autowired
    private ChickenOrderMapper chickenOrderMapper;

    @Override
    public IPage<ChickenCoupon> selectChickenCouponList(BasePage<ChickenCoupon> page, ChickenCoupon chickenCoupon) {
        try {
            IPage<ChickenCoupon> chickenCouponIPage = chickenCouponMapper.selectChickenCouponList(page, chickenCoupon);
            if (chickenCouponIPage.getSize() > 0) {
                for (ChickenCoupon record : page.getRecords()) {
                    if (record.getUseType() != null) {
                        record.setUseTypeName(EnumUtils.getEnum(ChickenCouponUseTypeEnums.class, EnumUtils.GET_ID, record.getUseType()).getName());
                    }
                    record.setStatusName(EnumUtils.getEnum(ChickenCouponStatusEnums.class, EnumUtils.GET_ID, record.getStatus()).getName());
                }
            }
            return chickenCouponIPage;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Map<String, Integer> getCouponNum(ChickenCoupon chickenCoupon) {
        // 封装Map
        Map<String, Integer> map = new HashMap<>();
        // 已失效
//        chickenCoupon.setStatus(ChickenCouponStatusEnums.INVALID.getId());
//        int invalidNum = chickenCouponMapper.countOrderNum(chickenCoupon);
//        map.put("invalidNum", invalidNum);
        // 冻结中
//        chickenCoupon.setStatus(ChickenCouponStatusEnums.FROZEN.getId());
//        int frozenNum = chickenCouponMapper.countOrderNum(chickenCoupon);
//        map.put("frozenNum", frozenNum);
        // 未使用
        chickenCoupon.setStatus(ChickenCouponStatusEnums.WAITUSE.getId());
        int waituseNum = chickenCouponMapper.countOrderNum(chickenCoupon);
        map.put("waituseNum", waituseNum);
        // 已使用=已预约待核销+已使用
        chickenCoupon.setStatus(ChickenCouponStatusEnums.WAITCONFIRM.getId());
        int waitconfirmNum = chickenCouponMapper.countOrderNum(chickenCoupon);
        // 已使用
        chickenCoupon.setStatus(ChickenCouponStatusEnums.USED.getId());
        int usedNum = chickenCouponMapper.countOrderNum(chickenCoupon);
        map.put("usedNum", usedNum + waitconfirmNum);
        // 已过期
        chickenCoupon.setStatus(ChickenCouponStatusEnums.OVERDUE.getId());
        int overdueNum = chickenCouponMapper.countOrderNum(chickenCoupon);
        map.put("overdueNum", overdueNum);
        // 已转赠
        chickenCoupon.setStatus(ChickenCouponStatusEnums.GIVING.getId());
        int giving = chickenCouponMapper.countOrderNum(chickenCoupon);
        map.put("giving", giving);
        return map;
    }

    @Override
    public void handleFrozenCoupon() {
        try {
            chickenCouponMapper.releaseCoupon();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ThrowJsonException("释放提鸡券成待使用状态失败");
        }
    }

    @Override
    public void setTime() {
        chickenCouponMapper.setTime();
    }

    @Override
    public void handleInvalidCoupon() {
        try {
            //查询失效的预约提鸡订单，获取失效的优惠券集合
            List<ChickenSubscribe> subscribeList = chickenSubscribeMapper.selectSubscribeInvalid();
            List<String> ccids = subscribeList.stream().map(ChickenSubscribe::getCcids).collect(Collectors.toList());
            ArrayList<Integer> ccidsList = new ArrayList<>();
            for (String item : ccids) {
                ccidsList.addAll(CalcuUtils.getInstance().getIntegerList(item.split(",")));
            }

            //1、处理已预约但过了预约日期没核销的 预约提鸡订单
            chickenSubscribeMapper.handleSubscribeInvalidCoupon();

            ChickenCoupon chickenCoupon = new ChickenCoupon();
            //2、处理失效的预约提鸡订单中的提鸡券
            if (ccidsList.size() > 0) {
                chickenCoupon.setRemarks("已预约核销超时失效");
                chickenCouponMapper.handleSubscribeInvalidCoupon(chickenCoupon, ccidsList);
            }

            //3、过了提鸡有效期
            chickenCoupon.setRemarks("提鸡券有效期已超过");
            chickenCouponMapper.handleInvalidCoupon(chickenCoupon);

        } catch (Exception e) {
            e.printStackTrace();
            throw new ThrowJsonException("变更提鸡券为已失效状态失败");
        }
    }

    @Override
    public String getMaxNumber(Integer cid) {
        return chickenCouponMapper.getMaxNumber(cid);
    }

    @Override
    public void handleReleaseCoupon() {
        List<ChickenCoupon> couponList = chickenCouponMapper.selectReleaseCoupon();
        for (ChickenCoupon item : couponList) {
            item.setStatus(ChickenCouponStatusEnums.WAITUSE.getId());
            chickenCouponMapper.updateById(item);

            QueryWrapper<ChickenOrder> wrapper = new QueryWrapper<>();
            wrapper.eq("order_code", item.getOrderCode());
            ChickenOrder selectOne = chickenOrderMapper.selectOne(wrapper);
            if (selectOne != null) {
                selectOne.setCouponNum(selectOne.getCouponNum() + 1);
                chickenOrderMapper.updateById(selectOne);
            }
        }
    }
}
